import 'package:flutter/material.dart';

class ScrollToTop extends StatefulWidget {
  const ScrollToTop({
    super.key,
    required this.child,
    required this.scrollController,
    this.scrollOffset,
    this.btnColor,
    this.textColor,
  });

  final Widget child;
  final ScrollController scrollController;
  final int? scrollOffset;
  final Color? btnColor;
  final Color? textColor;
  @override
  State<ScrollToTop> createState() => _ScrollToTopState();
}

class _ScrollToTopState extends State<ScrollToTop> {
  bool backToTop = false;
  @override
  void initState() {
    widget.scrollController.addListener(() {
      setState(() {
        backToTop =
            widget.scrollController.offset > widget.scrollOffset!.toInt()
                ? true
                : false;
      });
    });
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
    widget.scrollController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        widget.child,
      ],
    );
  }

  Widget buildBTT(
    ScrollController scrollController,
    bool backToTop,
    Color? btnColor,
    Color? textColor,
  ) {
    return backToTop
        ? Align(
            alignment: Alignment.topCenter,
            child: Padding(
              padding: const EdgeInsets.only(top: 10),
              child: FloatingActionButton.extended(
                backgroundColor: btnColor,
                onPressed: () {
                  scrollController.animateTo(
                    0,
                    duration: const Duration(milliseconds: 500),
                    curve: Curves.easeInOut,
                  );
                },
                label: Text(
                  'Back to Top',
                  style: TextStyle(color: textColor),
                ),
              ),
            ),
          )
        : const SizedBox.shrink();
  }
}
